home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Daemon / Unnamed1 / 40beta / MPMain.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  16KB  |  559 lines

  1. #include "MP.h"
  2.  
  3.  
  4. #include <utility/hooks.h>
  5. #include <intuition/sghooks.h>
  6.  
  7. #include <proto/locale.h>
  8.  
  9. long __stack = 4096;
  10.  
  11. UBYTE EnvFile[]   ="ENV:ModePro.pref",
  12.       EnvArcFile[]="ENVARC:ModePro.pref";
  13.        
  14. #define MPVER  "4.0"
  15. #define VER     4
  16. #define REV     0
  17.  
  18. UBYTE   Version[]   ="$VER: ModePro "MPVER" "__AMIGADATE__;
  19. UBYTE   ModePro[]   ="ModePro "MPVER;
  20.  
  21. BYTE PublicSignal;
  22. struct Process *MPTask;
  23.  
  24. struct Screen ASM *(*OldOpenScreen)(REG __a0 struct NewScreen *,
  25.                                     REG __a6 struct IntuitionBase *);
  26. struct Screen ASM *(*OldOpenScreenTagList)(REG __a0 struct ExtNewScreen *ns,
  27.                                            REG __a1 struct TagItem *taglist,
  28.                                            REG __a6 struct IntuitionBase * );
  29. BOOL ASM (*OldCloseScreen)(REG __a0 struct Screen *,
  30.                            REG __a6 struct IntuitionBase *);
  31. struct Window ASM *(*OldOpenWindow)(REG __a0 struct NewWindow *nw,
  32.                                     REG __a6 struct IntuitionBase *lib);
  33. struct Window ASM *(*OldOpenWindowTagList)(REG __a0 struct NewWindow *nw,
  34.                                            REG __a1 struct TagItem *tags,
  35.                                            REG __a6 struct IntuitionBase *lib);
  36. BOOL ASM *(*OldLayoutMenusA)(REG __a0 struct Menu *fm,
  37.                              REG __a1 APTR vi, 
  38.                              REG __a2 struct TagItem *tags,
  39.                              REG __a6 struct Library *lib);
  40.  
  41.  
  42.  
  43. BOOL    V39;
  44. ULONG   Deinstall=FALSE;
  45.                 
  46. struct NewBroker NewBroker=
  47. {
  48.   NB_VERSION,
  49.   "ModePro",
  50.   ModePro,
  51.   0,
  52.   NBU_UNIQUE | NBU_NOTIFY,
  53.   COF_SHOW_HIDE,
  54.   0,0,0
  55. };
  56.  
  57.  
  58.  
  59. UWORD NumDriPens;
  60.  
  61. UWORD defaultpens[]=
  62. {
  63.   0,1,1,2,1,3,1,0,2,1,2,1
  64. };
  65.  
  66. struct TextAttr Topaz8 = { "topaz.font",8,0,0};
  67.  
  68.  
  69.  
  70. struct MsgPort *BrokerPort,
  71.                *CatchPort,
  72.                *ReplyPort;
  73. struct Message *ms;
  74.  
  75. struct IntuitionBase *IntuitionBase; 
  76. struct GfxBase       *GfxBase;
  77. struct Library       *AslBase,
  78.                      *CxBase,
  79.                      *DiskfontBase,
  80.                      //*GadToolsBase,
  81.                      *IconBase,
  82.                      *IFFParseBase,
  83.                      *LocaleBase,
  84.                      *UtilityBase;
  85.  
  86. struct Libs MP_Libs[]=
  87. {
  88.   (APTR *)&AslBase,      "asl.library",        38,
  89.   (APTR *)&CxBase,       "commodities.library",36,
  90.   (APTR *)&DiskfontBase, "diskfont.library",   36,
  91. //  (APTR *)&GadToolsBase, "gadtools.library",   36,
  92.   (APTR *)&GfxBase,      "graphics.library",   36,
  93.   (APTR *)&IconBase,     "icon.library",       36,
  94.   (APTR *)&IFFParseBase, "iffparse.library",   36,
  95.   (APTR *)&IntuitionBase,"intuition.library",  36,
  96. //  (APTR *)&LocaleBase,   "locale.library",     38,
  97.   (APTR *)&UtilityBase,  "utility.library",    36,
  98.   0,0,0
  99. };
  100.  
  101. CxObj   *Broker;
  102. CxMsg   *cxmsg;
  103. ULONG   cxsig;
  104. ULONG   sigrcvd;
  105.  
  106. /*** Tooltypes ***/
  107.  
  108. UBYTE *Hotkey="lcommand p",
  109.       Popup=TRUE;
  110. BYTE  Priority=0;
  111. //UBYTE *UserFontName=0;
  112. //LONG  UserFontSize=8;
  113. STRPTR PrefsPath;
  114.  
  115. struct Catalog *Catalog;
  116.  
  117. struct MPSem MPSem;
  118.  
  119. void main(int argc, char **argv) 
  120. {
  121.   ULONG l;
  122.   struct Screen *ps;
  123.   struct DrawInfo *di;
  124.  
  125.   if(LocaleBase=OpenLibrary("locale.library",38))
  126.     Catalog=OpenCatalog(NULL,(STRPTR)"modeprodaemon.catalog", TAG_DONE);
  127.   else
  128.     Catalog=NULL;
  129.   
  130.   InitStrings();
  131.  
  132.   if(OpenLibs(ModePro,MP_Libs))
  133.   {
  134.   
  135.     NewBroker.nb_Descr=GetString(MSG_COMMODITY_TITLE);
  136.  
  137.     if(((struct Library *)IntuitionBase)->lib_Version>38)
  138.     {
  139.       NumDriPens=12;
  140.       V39=TRUE;
  141.     }
  142.     else
  143.     {
  144.       NumDriPens=9;
  145.       V39=FALSE;
  146.     }
  147.     if(GetArgs(argc,argv))
  148.     {
  149.       {
  150.         struct SignalSemaphore *sem;
  151.       
  152.         sem=&MPSem.SharedSem;
  153.         for(l=0;l<SEMAPHORES;l++)
  154.           InitSemaphore(&sem[l]);
  155.       }
  156.  
  157.       MPSem.SharedSem.ss_Link.ln_Name =(UBYTE *)"ModePro";
  158.       MPSem.SharedSem.ss_Link.ln_Pri  =0;
  159.       MPSem.Enabled =TRUE;
  160.       MPSem.Version =VER;
  161.       MPSem.Revision=REV;
  162.       MPSem.HotKey  =Hotkey;
  163.  
  164.       if(ps=LockPubScreen(NULL))
  165.       {
  166.         di=GetScreenDrawInfo(ps);
  167.         for(l=0;l<di->dri_NumPens && l<NumDriPens;l++)
  168.           defaultpens[l]=di->dri_Pens[l];      
  169.         FreeScreenDrawInfo(ps,di);
  170.         UnlockPubScreen(NULL,ps);
  171.       }
  172.   
  173.       if(BrokerPort=CreateMsgPort())
  174.       { 
  175.         NewBroker.nb_Pri=Priority;
  176.         NewBroker.nb_Port=BrokerPort;
  177.         if(Broker=CxBroker(&NewBroker,NULL))
  178.         {
  179.           AddHotKey(Broker,BrokerPort,Hotkey,EVT_HOTKEY);
  180.           if(CatchPort=CreateMsgPort())
  181.           {
  182.             if(MPSem.DaemonPort=CreateMsgPort())
  183.             {
  184.               if(ReplyPort=CreateMsgPort())
  185.               {
  186.                 if((PublicSignal=AllocSignal(-1))>=0)
  187.                 {
  188.                   MPTask=(struct Process *)FindTask(0);
  189.                   cxsig=1L<<BrokerPort->mp_SigBit;
  190.                   ActivateCxObj(Broker,1);
  191.         
  192.                   NewList(&MPSem.OpenList);
  193.                   NewList(&MPSem.PromotionList[0]);
  194.                   NewList(&MPSem.PromotionList[1]);
  195.                   
  196.                   if(!DaemonLoadFile(EnvFile))
  197.                       DaemonLoadFile(EnvArcFile);
  198.                   Forbid();
  199.                   OldCloseScreen       =SetFunction((struct Library *)IntuitionBase,-(0x42) ,(FUNCCAST)NewCloseScreen);
  200.                   OldOpenScreenTagList =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)NewOpenScreenTagList);
  201.                   OldOpenScreen        =SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)NewOpenScreen);
  202.                   OldOpenWindowTagList =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)NewOpenWindowTagList);
  203.                   OldOpenWindow        =SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)NewOpenWindow);
  204.                   OldLayoutMenusA      =SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)NewLayoutMenusA);
  205.                   Permit();
  206.                
  207.                   AddSemaphore((struct SignalSemaphore *)&MPSem);
  208.                   ProcessInput();
  209.                   RemSemaphore((struct SignalSemaphore *)&MPSem);
  210.                   
  211.                   ObtainSemaphore(&MPSem.ListSem);
  212.                   ReleaseSemaphore(&MPSem.ListSem);
  213.                   ObtainSemaphore(&MPSem.PrefsSem);
  214.                   ReleaseSemaphore(&MPSem.PrefsSem);
  215.  
  216.                   ProcessMPMessages();
  217.  
  218.                   {
  219.                     struct MPMessage *mpm;
  220.                     while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
  221.                       FreeVec(mpm);
  222.                   }
  223.  
  224.                   FreeDefList(&MPSem.PromotionList[0]);
  225.                   FreeDefList(&MPSem.PromotionList[1]);
  226.                   FreeSignal(PublicSignal);
  227.                 }
  228.                 DeleteMsgPort(ReplyPort);
  229.               }
  230.               DeleteMsgPort(MPSem.DaemonPort);
  231.             }
  232.             DeleteMsgPort(CatchPort);
  233.           }
  234.           DeleteCxObjAll(Broker);
  235.           while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
  236.             ReplyMsg((struct Message *)cxmsg);
  237.         }/* endif CxBroker() */
  238.         DeleteMsgPort(BrokerPort);
  239.       }// endif CreateMsgPort(BrokerPort)
  240.       // ArgArrayDone();
  241.       CleanupArgs(argc);
  242.     }// endif GetArgs() 
  243.     CloseLibs(MP_Libs);
  244.   } 
  245.   if(LocaleBase)
  246.   {
  247.     CloseCatalog(Catalog);
  248.     CloseLibrary(LocaleBase);
  249.   }
  250. }
  251.   
  252. void ProcessInput(void)
  253. {
  254.  
  255.   struct DefaultNode *catdnode;
  256.   ULONG  replysig,portsig,catchsig,signal,msgtype,pubsig,allsigs,daemonsig;
  257.   LONG msgid;
  258.  
  259.   
  260.   daemonsig=1<< MPSem.DaemonPort->mp_SigBit;
  261.   catchsig=1 << CatchPort->mp_SigBit;
  262.   portsig =1 << BrokerPort->mp_SigBit;
  263.   pubsig  =1 << PublicSignal; 
  264.   replysig=1 << ReplyPort->mp_SigBit;
  265.  
  266.   allsigs=daemonsig | replysig | pubsig | catchsig | portsig | SIGBREAKF_CTRL_C;
  267.  
  268.   if(Popup)
  269.     ShowGUI();
  270.  
  271.   while (!Deinstall)
  272.   {
  273.     signal=Wait (allsigs);
  274.  
  275.     if(SIGBREAKF_CTRL_C & signal)
  276.       CheckToQuit();
  277.  
  278.     if(daemonsig & signal)
  279.       ProcessMPMessages();
  280.  
  281.     if(replysig & signal)
  282.     {
  283.       struct MPMessage *mpm;
  284.       
  285.       while(mpm=(struct MPMessage *)GetMsg(ReplyPort))
  286.         FreeVec(mpm);
  287.     }
  288.  
  289.     if(pubsig & signal)
  290.     {
  291.       SetSignal(0,pubsig);
  292.       ClosePubScreens();
  293.     }
  294.  
  295.     if(catchsig & signal)
  296.     {
  297.       while(catdnode=(struct DefaultNode *)GetMsg(CatchPort))
  298.       {
  299.         AddDNode(catdnode->Type,catdnode);
  300.       }
  301.       if(MPSem.CatchPopup)
  302.         ShowGUI();
  303.     }
  304.         
  305.     if(portsig & signal)
  306.     {
  307.       while(cxmsg=(CxMsg *)GetMsg(BrokerPort))
  308.       {
  309.         msgid=CxMsgID(cxmsg);
  310.         msgtype=CxMsgType(cxmsg);
  311.         ReplyMsg((struct Message *)cxmsg);     
  312.         switch(msgtype)
  313.         {
  314.           case CXM_IEVENT:
  315.             switch(msgid)
  316.             {
  317.               case EVT_HOTKEY:
  318.                 ShowGUI();
  319.                 break;
  320.               default:
  321.                 {
  322.                   struct OpenNode *on;
  323.                   
  324.                   ObtainSemaphoreShared(&MPSem.ListSem);
  325.                   on=(struct OpenNode *)MPSem.OpenList.lh_Head;
  326.                   while(on->on_Node.ln_Succ)
  327.                   {
  328.                     if(on=(struct OpenNode *)msgid)
  329.                     {
  330.                       ScreenToFront(on->Screen);
  331.                       DeleteCxObjAll(on->Cx);
  332.                       on->Cx=AddHotKey(Broker,BrokerPort,on->HotKey,(ULONG)on);
  333.                       break;
  334.                     }
  335.                     on=(struct OpenNode *)on->on_Node.ln_Succ;
  336.                   }
  337.                   ReleaseSemaphore(&MPSem.ListSem);
  338.                 }
  339.             }
  340.             break;
  341.           case CXM_COMMAND:
  342.             switch(msgid)
  343.             {
  344.               case CXCMD_DISABLE:
  345.                 MPSem.Enabled=FALSE;
  346.                 SendPrefsMsg(MP_REFRESH,0,0);
  347.                 break;
  348.               case CXCMD_ENABLE:
  349.                 MPSem.Enabled=TRUE;
  350.                 SendPrefsMsg(MP_REFRESH,0,0);
  351.                 break;
  352.               case CXCMD_KILL:
  353.                 CheckToQuit();
  354.                 break;
  355.               case CXCMD_UNIQUE:
  356.               case CXCMD_APPEAR:
  357.                 ShowGUI();
  358.                 break;
  359.               case CXCMD_DISAPPEAR:
  360.                 HideGUI();
  361.                 break;
  362.             }
  363.             break;        
  364.         }
  365.       }
  366.     }
  367.   }
  368.   HideGUI();
  369. }    
  370.  
  371. void ProcessMPMessages(void)
  372. {
  373.   struct MPMessage *mpm;
  374.  
  375.   while(mpm=(struct MPMessage *)GetMsg(MPSem.DaemonPort))
  376.   {
  377.     //printf("DaemonMsg Command %d  Code %d  Data %8x\n",mpm->Command,mpm->Code,mpm->Data);
  378.     
  379.     switch(mpm->Command)
  380.     {
  381.       case MP_QUIT:
  382.         CheckToQuit();
  383.         break;
  384.         /*
  385.       case MP_NEWNODE:
  386.         AddDNode(mpm->Code,mpm->Data);
  387.         break;
  388.       case MP_REMNODE:
  389.         RemDNode(mpm->Data);
  390.         break;
  391.         */      
  392.     }
  393.     ReplyMsg((struct Message *)mpm);
  394.   }
  395. }
  396.    
  397.  
  398. void ClosePubScreens(void)  
  399. {
  400.   struct OpenNode *on,*on2;
  401.   
  402.   ObtainSemaphore(&MPSem.OpenListSem);
  403.   
  404.   on=(struct OpenNode *)MPSem.OpenList.lh_Head;
  405.   
  406.   while(on->on_Node.ln_Succ)
  407.   {
  408.     if(on->Flags & ON_PUBLIC && !(on->Flags & ON_OPEN))
  409.     {
  410.       if(OldCloseScreen(on->Screen,IntuitionBase))
  411.       {
  412.         on2=(struct OpenNode *)on->on_Node.ln_Pred;
  413.         Remove((struct Node *)on);
  414.         FreeOpenNode(on);
  415.         on=on2;
  416.       }
  417.     }
  418.     on=(struct OpenNode *)on->on_Node.ln_Succ;
  419.   }
  420.  
  421.   ReleaseSemaphore(&MPSem.OpenListSem);
  422. }
  423.  
  424.  
  425. void CheckToQuit(void)
  426. {
  427.   APTR  CheckCloseScreen,
  428.         CheckOpenScreen,
  429.         CheckOpenScreenTagList,
  430.         CheckOpenWindow,
  431.         CheckOpenWindowTagList,
  432.         CheckLayoutMenusA;
  433.   BOOL patchedover=FALSE,
  434.        screensopen=FALSE;
  435.   ULONG len;
  436.   UBYTE *warning;
  437.   UBYTE *warn;
  438.   
  439.   ObtainSemaphoreShared(&MPSem.ListSem);
  440.   ObtainSemaphore(&MPSem.OpenListSem);
  441.   Forbid();
  442.   
  443.   if(IsListEmpty(&MPSem.OpenList))
  444.   {
  445.     /**** check LVOs and make sure their mine ****/
  446.     /**** get LVOs,so put mine in thier, and get the result ****/ 
  447.     CheckCloseScreen        =SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)OldCloseScreen);
  448.     CheckOpenScreenTagList  =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)OldOpenScreenTagList);
  449.     CheckOpenScreen         =SetFunction((struct Library *)IntuitionBase,-(0xc6), (FUNCCAST)OldOpenScreen);
  450.     CheckOpenWindowTagList  =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)OldOpenWindowTagList);
  451.     CheckOpenWindow         =SetFunction((struct Library *)IntuitionBase,-(0xcc), (FUNCCAST)OldOpenWindow);
  452.     CheckLayoutMenusA       =SetFunction((struct Library *)GadToolsBase ,-(0x42), (FUNCCAST)OldLayoutMenusA);
  453.   
  454.     /**** now restore the LVOs to whatever they were ***/
  455.    
  456.     if((APTR)CheckCloseScreen       != (APTR)NewCloseScreen       ||
  457.        (APTR)CheckOpenScreen        != (APTR)NewOpenScreen        || 
  458.        (APTR)CheckOpenScreenTagList != (APTR)NewOpenScreenTagList ||
  459.        (APTR)CheckOpenWindow        != (APTR)NewOpenWindow        ||
  460.        (APTR)CheckOpenWindowTagList != (APTR)NewOpenWindowTagList ||
  461.        (APTR)CheckLayoutMenusA      != (APTR)NewLayoutMenusA)
  462.     {                 
  463.       SetFunction((struct Library *)IntuitionBase,-(0x42), (FUNCCAST)CheckCloseScreen);
  464.       SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)CheckOpenScreenTagList);
  465.       SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)CheckOpenScreen);
  466.       SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)CheckOpenWindowTagList);
  467.       SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)CheckOpenWindow);  
  468.       SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)CheckLayoutMenusA);
  469.       
  470.       patchedover=TRUE;
  471.     }
  472.     else
  473.     {
  474.       Deinstall=TRUE;
  475.       SendPrefsMsg(MP_QUIT,0,0);
  476.     }
  477.   }
  478.   else
  479.   {
  480.     struct OpenNode *on;
  481.   
  482.     warn=GetString(MSG_REQ_CANT_QUIT_SCREENS);
  483.     
  484.     len=strlen(warn);
  485.     
  486.     on=(struct OpenNode *)MPSem.OpenList.lh_Head;
  487.     while(on->on_Node.ln_Succ)
  488.     {
  489.       len+=strlen(on->Screen->Title)+4;
  490.       on=(struct OpenNode *)on->on_Node.ln_Succ;
  491.     }
  492.     if(warning=AllocVec(len,MEMF_CLEAR|MEMF_NO_EXPUNGE))    
  493.     {
  494.       strcpy(warning,warn);
  495.       on=(struct OpenNode *)MPSem.OpenList.lh_Head;
  496.       while(on->on_Node.ln_Succ)
  497.       {
  498.         strcat(warning,"\n  ");
  499.         strcat(warning,on->Screen->Title);
  500.         on=(struct OpenNode *)on->on_Node.ln_Succ;
  501.       }
  502.       screensopen=TRUE;
  503.     }
  504.   }
  505.   ReleaseSemaphore(&MPSem.OpenListSem);
  506.   ReleaseSemaphore(&MPSem.ListSem);
  507.   
  508.   Permit();
  509.   
  510.   if(patchedover)
  511.     EZReq(0,0,ModePro,GetString(MSG_REQ_CANT_QUIT_PATCHED_OVER),GetString(MSG_REQ_OK),0);   
  512.   
  513.   if(screensopen && warning)
  514.   {
  515.     EZReq(0,0,ModePro,warning,GetString(MSG_REQ_OK),0);
  516.     FreeVec(warning);
  517.   }
  518. }
  519.  
  520. LONG SendPrefsMsg(ULONG Command, ULONG Code, APTR Data)
  521. {
  522.   LONG rv=-1;
  523.   struct MPMessage *mpm;
  524.  
  525.   ObtainSemaphore(&MPSem.PortSem);
  526.   if(MPSem.PrefsPort)
  527.   {
  528.     rv=0;
  529.     if(mpm=AllocVec(sizeof(struct MPMessage),MEMF_PUBLIC|MEMF_CLEAR))
  530.     {
  531.       mpm->Command=Command;
  532.       mpm->Code=Code;
  533.       mpm->Data=Data;
  534.       PutMsg(MPSem.PrefsPort,(struct Message *)mpm);
  535.       rv=1;
  536.     }
  537.   }
  538.   ReleaseSemaphore(&MPSem.PortSem);
  539.   return(rv);
  540. }
  541.  
  542. BOOL DaemonLoadFile(STRPTR FileName)
  543. {
  544.   LONG error;
  545.   BOOL rv;
  546.  
  547.   // no need for protection, the lists are not public
  548.   // when this function is called.  
  549.   rv=LoadFile(MPSem.PromotionList,FileName);
  550.   if(error=IoErr())
  551.   {
  552.     UBYTE errstr[91];
  553.  
  554.     Fault(error,GetString(MSG_REQ_LOAD_ERROR),errstr,90);
  555.     EZReq(0,0,ModePro,errstr,GetString(MSG_REQ_OK),0);
  556.   }
  557.   return(rv);
  558. }    
  559.